#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/1/25 21:55
# @Author  : FLC
# @Contact : beifeng107@foxmail.com
# @File    : log_tool.py
# @Software: PyCharm
import datetime
import logging
import os
from interface.api_object.utils.file_tool import FileTool

# 初始化 logger 对象
logger = logging.getLogger(__name__)

# 输出日志到控制台的 handler
console_handler = logging.StreamHandler()

# 按时间戳生成日志文件名
log_file_name = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")+".log"
# 拼接日志文件完整路径
log_file_path = os.sep.join([FileTool.get_project_dir(), "logs", log_file_name])
# 输出日志到文件的 handler
file_handler = logging.FileHandler(filename=log_file_path, encoding="utf-8")

# 设置日志输出的格式
formatter = logging.Formatter("%(asctime)s %(levelname)s %(filename)s[line:%(lineno)d]：%(message)s", "%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(formatter)
file_handler.setFormatter(formatter)

# 添加handler
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# 设置日志显示级别
logger.setLevel(logging.DEBUG)